<?php

	$rawTweet = "Something bad happened...";
	if (isset($_REQUEST['rawtweet']))
		$rawTweet = stripslashes($_REQUEST['rawtweet']);

	@session_start();
	$access_token = $_SESSION['access_token'];
	$suser = $access_token['user_id'];

// build an array of all possible files
// use the arry max to rand
//build the image from that array

	include ("components/_visuals.php");
	
	$registry = getRegistry($suser);

	
	$rootdir = "/home/glidecas/public_html/chirplace1.0.0.0.com";
	$basedir = $rootdir."/paths";

	$max = count($registry);

	$randomnum = rand(1, $max)-1;

	if (isset($_REQUEST['force']))
	{
		$randomnum = ($_REQUEST['force']);
	}

include ($registry[$randomnum][1]);

$map = $registry[$randomnum][0];
header("Content-type: image/jpeg");	
$img = ImageCreateFromjpeg($map);


//some images require wrapping, not char by char placement.
// for those images, 1 line will appear in the array
if (count($pathinfo)>1)
{
	$strlen = min(140, strlen($rawTweet));
	for($i=0;$i<$strlen;$i++)
	{
	
		$color = $pathinfo[$i][4];
		$int = hexdec($color);
		$arr = array("red" => 0xFF & ($int >> 0x10),
		"green" => 0xFF & ($int >> 0x8),
		"blue" => 0xFF & $int);
	
		$clr = ImageColorAllocate($img, $arr["red"], $arr["green"], $arr["blue"]); 

		imagettftext($img, $pathinfo[$i][0], $pathinfo[$i][1], $pathinfo[$i][2], $pathinfo[$i][3], $clr, $basedir."/".$pathinfo[$i][5], $rawTweet[$i]);
		
	}
}
else
{
	//single line
	$color = $pathinfo[0][4];
	$int = hexdec($color);
	$arr = array("red" => 0xFF & ($int >> 0x10),
		"green" => 0xFF & ($int >> 0x8),
		"blue" => 0xFF & $int);
	
	$clr = ImageColorAllocate($img, $arr["red"], $arr["green"], $arr["blue"]); 
	$xandwidth = explode(",", $pathinfo[0][2]);


	imagettftextbox($img, $pathinfo[0][0], $pathinfo[0][1], $xandwidth[0], $pathinfo[0][3], $clr, $basedir."/".$pathinfo[0][5], $rawTweet, $xandwidth[1]);
}

//since I know the name of the image, I can return it in the header.
//header("Content-Disposition: inline; filename=".imgtxt.php.".jpg");

//if (substr($map, -3) == "jpg")
	Imagejpeg($img); 


ImageDestroy($img);  // 


define("ALIGN_LEFT", "left");
define("ALIGN_CENTER", "center");
define("ALIGN_RIGHT", "right");

function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width)
{
        $text_lines = explode("\n", $text); // Supports manual line breaks!
       
        $lines = array();
        $line_widths = array();
       
        $largest_line_height = 0;
       
        foreach($text_lines as $block)
        {
            $current_line = ''; // Reset current line
           
            $words = explode(' ', $block); // Split the text into an array of single words
           
            $first_word = TRUE;
           
            $last_width = 0;
           
            for($i = 0; $i < count($words); $i++)
            {
                $item = $words[$i];
                $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item);
                $line_width = $dimensions[2] - $dimensions[0];
                $line_height = $dimensions[1] - $dimensions[7];
               
                if($line_height > $largest_line_height) $largest_line_height = $line_height;
               
                if($line_width > $max_width && !$first_word)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $last_width ? $last_width : $line_width;
                   
                    /*if($i == count($words))
                    {
                        continue;
                    }*/
                   
                    $current_line = $item;
                }
                else
                {
                    $current_line .= ($first_word ? '' : ' ') . $item;
                }
               
                if($i == count($words) - 1)
                {
                    $lines[] = $current_line;
                   
                    $line_widths[] = $line_width;
                }
               
                $last_width = $line_width;
                   
                $first_word = FALSE;
            }
           
            if($current_line)
            {
                $current_line = $item;
            }
        }
       
        $i = 0;
        foreach($lines as $line)
        {
            if($align == ALIGN_CENTER)
            {
                $left_offset = ($max_width - $line_widths[$i]) / 2;
            }
            elseif($align == ALIGN_RIGHT)
            {
                $left_offset = ($max_width - $line_widths[$i]);
            }
            imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line);
            $i++;
        }
       
        return $largest_line_height * count($lines);
}
?> 
 
